*****************************************************************
* This file prepares a concordance for product codes by HS6 
*****************************************************************

clear all
set more off

cd ${data_dir}

* Start from raw trade data 
use firmtrade, clear

* keep unique product codes by year
gen productcode = substr(vare,1,6)
bys aar productcode: keep if _n == 1
keep aar productcode vrd

* prepare merge with correspondence tables
gen h1_code = productcode if aar == "1997" | aar == "1998" | aar == "1999" | aar == "2000" | aar == "2001"
gen h2_code = productcode if aar == "2002" | aar == "2003" | aar == "2004" | aar == "2005" | aar == "2006"
gen h3_code = productcode if aar == "2007" | aar == "2008" | aar == "2009" | aar == "2010"

sort h1_code
merge n:1 h1_code using HS1996_1986
drop if _merge == 2
drop _merge

sort h2_code
merge n:1 h2_code using HS2002_1986
drop if _merge == 2
drop _merge

sort h3_code
merge n:1 h3_code using HS2007_1986
drop if _merge == 2
drop _merge

gen h0_code = h0_1996 if aar == "1997" | aar == "1998" | aar == "1999" | aar == "2000" | aar == "2001"
replace h0_code = h0_2002 if aar == "2002" | aar == "2003" | aar == "2004" | aar == "2005" | aar == "2006"
replace h0_code = h0_2007 if aar == "2007" | aar == "2008" | aar == "2009" | aar == "2010"

keep aar productcode h0_code
sort aar productcode h0_code
save firmtrade_HS1986_table, replace
